package com.graduate.back.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.graduate.back.pojo.MyUser;
import com.graduate.common.entity.Reserve;
import com.graduate.common.entity.Result;
import com.graduate.common.entity.Users;
import com.graduate.common.exception.UserException;
import com.graduate.common.vo.HouseVo;
import com.graduate.common.vo.ReserveVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author bwzfy
 * @create 2021/5/13
 **/
@Controller
@RequestMapping("/reserve")
public class ReserveController extends BaseController {

    /**
     * 分页展示所有的预定信息
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public Result<IPage<ReserveVo>> houseList(@RequestParam(required = false) Map<String, Object> map) {
        Result<IPage<ReserveVo>> result = new Result<>();
        result.setCode(200);
        result.setData(reserveService.selectReservePage(map));
        return result;
    }

    /**
     * 普通方式预定/vip方式预定
     * 把当前的用户预订信息存入数据库中
     * @return
     */
    @ResponseBody
    @PostMapping("/house")
    public Result<String> reserveHouse(@RequestBody Reserve reserve, HttpSession session) {
        // 从session中获取用户信息
        MyUser user = (MyUser) session.getAttribute("user");
        reserve.setUserId(user.getId());
        Result<String> result = new Result<>();
        // 普通预定房间
        try {
            reserveService.reserveHouse(reserve);
            result.setCode(200);
            return result;
        } catch (UserException e) {
            e.printStackTrace();
            // 把异常信息封装入返回信息中
            result.setCode(500);
            result.setData(e.getMessage());
        }
        return result;
    }
}
